package com.dlc.apk.service.user.impl

import com.dlc.apk.api.ApiConstants
import com.dlc.apk.api.ApiManager
import com.dlc.apk.api.BaseResponseModel
import com.dlc.apk.api.entity.AdData
import com.dlc.apk.api.entity.AppVersionResponseData
import com.dlc.apk.api.entity.DeviceInfoResponseData
import com.dlc.apk.service.user.IUserService
import com.dlc.apk.ui.model.*
import com.dlc.apk.utils.CacheHelper
import com.dlc.apk.utils.DeviceUtil
import com.dlc.apk.utils.GsonUtil
import com.dlc.apk.utils.LogPlus
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

class UserService : IUserService {
    override fun loadUsers(appForm: AppForm) {
        LogPlus.e("###开始登录...")
    }

    override fun getDeviceInfo(deviceInfoModel: DeviceInfoModel) {
        deviceInfoModel.onProcessListener.onStart()
        ApiManager.instance.apiService
            .deviceInfo()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<BaseResponseModel<DeviceInfoResponseData>> {
                override fun onSubscribe(d: Disposable) {
                }

                override fun onNext(t: BaseResponseModel<DeviceInfoResponseData>) {
                    LogPlus.e("###deviceInfo data->" + t.data)
                    if (t.code != ApiConstants.CODE_SUCCESS) {
                        deviceInfoModel.onProcessListener.onFailure(java.lang.RuntimeException(t.msg), t.msg)
                        deviceInfoModel.onProcessListener.onComplete()
                        return
                    }
                    t.data?.run {
                        logo = ApiConstants.getFullUrl(logo)
                        deviceInfoModel.logo.set(logo)
                        deviceInfoModel.name.set(device_name + "(编号:${DeviceUtil.androidId})")
                        deviceInfoModel.address.set(address)
                    }
                    CacheHelper.setDeviceInfo(t.data)
                    deviceInfoModel.onProcessListener.onSuccess()
                    deviceInfoModel.onProcessListener.onComplete()
                }

                override fun onError(e: Throwable) {
                    deviceInfoModel.onProcessListener.onFailure(e, e.message)
                    deviceInfoModel.onProcessListener.onComplete()
                }

                override fun onComplete() {
                }

            })
    }

    override fun getAdBanners(adForm: AdForm) {
        adForm.onProcessListener.onStart()
        ApiManager.instance.apiService
            .getAdList()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<BaseResponseModel<List<AdData>>> {
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(t: BaseResponseModel<List<AdData>>) {
                    if (t.code != ApiConstants.CODE_SUCCESS) {
                        adForm.onProcessListener.onFailure(RuntimeException(t.msg), t.msg)
                        adForm.onProcessListener.onComplete()
                        return
                    }
                    LogPlus.e("###data -> " + GsonUtil.instance.parseObjToJsonStr(t))
                    val adItems = ArrayList<AdForm.AdItem>()
                    t.data?.forEachIndexed { index, adData ->
                        val type = if (adData.type == 1) {
                            AdForm.typeImage
                        } else AdForm.typeVideo
                        val adItem = AdForm.AdItem(type, ApiConstants.getFullUrl(adData.img), 10)
                        adItem.startTimestamp = adData.start_date * 1000//接口是秒
                        adItem.endTimestamp = adData.end_date * 1000//接口是秒
                        adItem.startTime = adData.stime + ":00"//08:00
                        adItem.endTime = adData.etime + ":00"//20:00
                        adItem.adId = adData.id.toString()
                        adItems.add(adItem)
                    }
                    //保存广告数据到本地
                    CacheHelper.saveAdResponseData(t.data)

                    adForm.adItems.set(adItems)
                    adForm.onProcessListener.onSuccess()
                    adForm.onProcessListener.onComplete()
                }

                override fun onError(e: Throwable) {
                    LogPlus.e("###请求出错->" + LogPlus.getStackTraceInfo(e))
                    adForm.onProcessListener.onFailure(e, e.message)
                    adForm.onProcessListener.onComplete()
                }

                override fun onComplete() {}
            })
    }

    override fun getAppVersion(model: AppVersion) {
        model.onProcessListener.onStart()
        ApiManager.instance.apiService
            .getAppVersion()
            .retry(1)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<BaseResponseModel<AppVersionResponseData>> {
                override fun onSubscribe(d: Disposable) {}
                override fun onNext(t: BaseResponseModel<AppVersionResponseData>) {
                    if (t.code != ApiConstants.CODE_SUCCESS) {
                        model.onProcessListener.onFailure(RuntimeException(t.msg), t.msg)
                        model.onProcessListener.onComplete()
                        return
                    }
                    model.appVersionResponseData = t.data
                    model.onProcessListener.onSuccess()
                    model.onProcessListener.onComplete()
                }

                override fun onError(e: Throwable) {
                    LogPlus.e("###请求出错->" + LogPlus.getStackTraceInfo(e))
                    model.onProcessListener.onFailure(e, e.message)
                    model.onProcessListener.onComplete()
                }

                override fun onComplete() {}
            })
    }
}